#! /usr/bin/env python
# -*- coding: utf-8 -*-


import pygame

from position import *
from gameUtils import *

try:
	import Numeric as N
	import pygame.surfarray as surfarray
except ImportError:
	raise ImportError, "Numeric and Surfarray are required."

class Dirt(pygame.sprite.Sprite):
	hidden_img=None
	def __init__(self,parent,floorName,fscale_Xmax):
		try:
			import psyco
			psyco.full()
		except ImportError:
			pass
			
		psyco.log()
		psyco.profile(0.2)
		
		u"""Dirt Class"""
		pygame.sprite.Sprite.__init__(self, self.containers) # Sprite inicialization
		
		self.parent=parent.parent
		self.floor=parent
		
		self.floorName=floorName
		self.originalSurface=load_transparency(self.floorName+'_dirt.png')
		self.originalSurfaceArray=surfarray.array3d(self.originalSurface)
		self.load()
		
		
		xmax=self.surface.get_size()[0]
		self.scale=float(xmax)*fscale_Xmax

	def redirt(self,percent=1.):
		for x in range(self.surface.get_size()[0]):
			for y in range(self.surface.get_size()[1]):
				going2dirt = self.originalSurface.get_at((x,y))[3]
				if going2dirt > 0:
					current = self.surface.get_at((x,y))
					if current[3]+percent*going2dirt>going2dirt:
						value=(current[0],current[1],current[2],going2dirt)
					else:
						value=(current[0],current[1],current[2],int(current[3]+percent*going2dirt))
					
					self.surface.set_at((x,y),value)
				

	def load(self):
		self.surface=self.originalSurface.copy()
	
	def getLarguraPx(self):
		return self.surface.get_size()[0]
	
	def getAlturaPx(self):
		return self.surface.get_size()[1]
		
	def getLargura(self):
		return self.getLarguraPx()*self.scale
	
	def getAltura(self):
		return self.getAlturaPx()*self.scale
	
	def isCleanable_px(self,pos):
		try:
			return self.originalSurface.get_at(pos)[3] !=0
		except:
			return False
	
	def isClean_px(self,(x,y)):
		return self.surface.get_at((x,y))[3] == 0
	
	def clean(self,pos):
		ipos=(pos.x,self.floor.getAltura()-pos.y)
		self.clean_px(resize(ipos,self.scale))
		
	def clean_area(self,pos,area):
		self.surface.lock()
		ipos=(pos.x,self.floor.getAltura()-pos.y)
		x,y=resize(ipos,self.scale)
		
		for (dx,dy),value in area.points.items():
			self.clean_px((x+dx,y+dy))
		self.surface.unlock()

	def clean_area_tree(self,pos,area_tree):
		self.surface.lock()
		
		pos0=resize((pos.x,self.floor.getAltura()-pos.y),self.scale)
		cps=[area_tree.iCP]
		
		cps_visited=[]
		
		cleanpoints=0
		while(len(cps)>0):
			cp=cps.pop(0)
			if cp not in cps_visited:
				cps_visited.append(cp)
				pos2Clean = (pos0[0]+cp.pos[0],pos0[1]+cp.pos[1])
				
				if self.isCleanable_px(pos2Clean):
					self.clean_px(pos2Clean,6*(10-cp.dist))
					cps+=cp.connections
					cleanpoints+=1
		self.surface.unlock()
		
		#~ print cleanpoints
		
	def getPositionIn(self,x,y):
		ymax=self.getAlturaPx()
		
		return Position(float(x)/self.scale,float(ymax-y)/self.scale)

	def clean_px(self,(x,y),value=None):
		if value==None:
			self.surface.set_at((x,y),(0,0,0,0))
		else:
			try:
				color=self.surface.get_at((x,y))
				try:
					self.surface.set_at((x,y),(int(color[0]),int(color[1]),int(color[2]),int(color[3]-value)))
				except:
					self.surface.set_at((x,y),(color[0],color[1],color[2],0))
			except:
				pass
	def sum(self):
		value=0
		
		for x in range(self.surface.get_width()):
			for y in range(self.surface.get_height()):
				value+=self.surface.get_at((x,y))[1]
		
		return value
	
	def mean(self):
		return self.sum()/self.surface.get_width()/self.surface.get_height()
		
	def update(self,visible):
		if visible:
			self.image=pygame.transform.smoothscale(self.surface, self.parent.screen.get_size())
		else:
			self.image=self.hidden_img
			
		self.rect = self.image.get_rect()
		#~ aux=self.rect.inflate(200, 200).fit(pygame.Rect(0, 0, 400, 400))
		#~ self.rect=aux


